#include "stdafx.h"
#include "tilerenderer.h"
#include <data/map/tile.hpp>
#include <QtOpenGL/qgl.h>

namespace Graphics {

static const uint SPLINE_SIZE = 4;

TileRenderer::TileRenderer()
{
}

TileRenderer::~TileRenderer()
{
}

void TileRenderer::render(const Data::ITile &tile) const
{
    //tile.ground().bind();
    renderTileByLines(tile);
}

void TileRenderer::renderTileByLines(const Data::ITile &tile) const
{
    static const uint FREQ = Data::ITile::KEYPOINTS_FREQUENCE;
    const float STEP = 1.0f / static_cast<float>(FREQ - 1);

    glBegin(GL_LINE_STRIP);
    for (uint x = 0; x < FREQ - 1; ++x)
        for (uint y = 0; y < FREQ; ++y) {
            glVertex3f(STEP * x,
                       STEP * y,
                       tile.keyPoint(x + FREQ * y));
            glVertex3f(STEP * (x + 1),
                       STEP * y,
                       tile.keyPoint(x + 1 + FREQ * y));
        }
//    glVertex3d(0, 0, 0);
//    glVertex3d(1, 0, 0);
//    glVertex3d(1, 1, 0);
//    glVertex3d(0, 1, 0);
#if 0
    for (uint i = 0; i != STEPS_COUNT; ++i)
        for (uint j = 0; j <= STEPS_COUNT; ++j) {
            double x1, x2, y, h1, h2;
            x1 = STEP * i;
            x2 = STEP * (i + 1);
            y =  STEP * j;

            h1 = getExactHeight(x1, y);
            h2 = getExactHeight(x2, y);

           // glTexCoord2d(x1, y);
            glVertex3d(x1, y, h1);
          //  glTexCoord2d(x2, y);
            glVertex3d(x2, y, h2);
        }
    for (uint i = 0; i < Data::ITile::KEYPOINTS_FREQUENCE; ++i)
        for (uint j = 0; j < Data::ITile::KEYPOINTS_FREQUENCE; ++j) {
            float h1 = _tile->keyPoint(4 * i + j);
            glVertex3d(0.33 * i, 0.33 * j, h1);
        }
#endif
    glEnd();
}

} // namespace Graphics
